package model.os.pagereplacement;

import java.util.LinkedList;

import model.os.PageTableEntry;

public class FifoW2Chance implements PageReplacementStrategy {

	private LinkedList<PageTableEntry> queue;

	public FifoW2Chance() {
		queue = new LinkedList<PageTableEntry>();
	}

	public int getPageToReplace() {

		PageTableEntry entry;

		while (true) {
			
			entry = queue.getFirst();
			
			if (entry.isReferenced()) {
				entry.setReferenced(false);
				queue.addLast(queue.removeFirst());
			} else{
				entry.setValid(false);
				return queue.removeFirst().getPageFrameNumber();
			}
		}
		
	}

	public void addPage(PageTableEntry entry) {
		queue.add(entry);
	}
	
	public void refferenced(PageTableEntry entry){
		entry.setReferenced(true);
	}

	public String getName() {
		return "FIFO W2C";
	}

}
